﻿using Microsoft.AspNetCore.Mvc;
using System.Diagnostics.CodeAnalysis;

namespace Shop.Core.Share
{
    /// <summary>
    /// 结果包装类
    /// </summary>
    /// <typeparam name="TValue"></typeparam>
    public class AppResult<TValue>
    {

        public AppResult()
        {
        }

        public AppResult(TValue value)
        {
            Data = value;
        }

        public AppResult(int code, string msg)
        {
            Code = code;
            Message = msg;
        }

        public AppResult(int code, string msg, TValue value)
        {
            Code = code;
            Message = msg;
            Data = value;
        }

        public AppResult(int code, string msg, TValue value, string error)
        {
            Code = code;
            Message = msg;
            Data = value;
            Errors = new string[] { error };
        }


        public int Code { get; set; } = 200;

        public string Message { get; set; } = "成功!";

        public TValue Data { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string[] Errors { get; set; } = Array.Empty<string>();

        public static implicit operator AppResult<TValue>(AppResult result)
        {
            return new AppResult<TValue>
            {
                Code = result.Code,
                Message = result.Message,
                Errors = result.Errors
            };
        }

        public static implicit operator AppResult<TValue>([NotNull]ProblemDetails problemDetails)
        {
            return new AppResult<TValue>
            {
                Code = problemDetails.Status ?? 400,
                Message = "失败",
                Errors = new string[] { problemDetails.Detail ?? string.Empty }
            };
        }

        public static implicit operator AppResult<TValue>(TValue value)
        {
            return new AppResult<TValue>(value);
        }
    }


    public class AppResult
    {

        public AppResult()
        {
        }

        public AppResult(int code, string msg)
        {
            Code = code;
            Message = msg;
        }

        public AppResult(int code, string msg,string error)
        {
            Code = code;
            Message = msg;
            Errors = new string[] { error };
        }

        public int Code { get; set; } = 200;

        public string Message { get; set; } = "成功!";

        /// <summary>
        /// 错误信息
        /// </summary>
        public string[] Errors { get; set; } = Array.Empty<string>();

        public static implicit operator AppResult([NotNull] ProblemDetails problemDetails)
        {
            return new AppResult
            {
                Code = problemDetails.Status ?? 400,
                Message = "失败",
                Errors = new string[] { problemDetails.Detail ?? string.Empty },
            };
        }
    }
}
